/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			helpviewer.cc
  \date			Jan 2013
  \author		TNick

  \brief		Contains the implementation of HelpViewer class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"helpviewer.h"

#include	<QProcess>
#include	<QByteArray>
#include	<QStringList>
#include	<QMessageBox>

#include	<cpg/cpg_debug.h>
#include	<cpg/cpg_config.h>
#include	<cpg/settings/apppaths.h>


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
HelpViewer::HelpViewer	( void )
	: QObject(), MemTrack()
{
	proc_ = NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
HelpViewer::~HelpViewer	( void )
{
	if ( proc_ != NULL )
	{
		if ( proc_->state() == QProcess::Running )
		{
			proc_->terminate();
			proc_->waitForFinished( 3000 );
		}
		delete proc_;
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::showDocumentation	( const QString & page )
{
	if ( startAssistant() == false )
		return;

	QByteArray ba("SetSource ");
	ba.append( "qthelp://cad-play-ground.org.cad-play-ground.1.0/cad-play-ground/ignore-docs/" );
	ba.append( page.toLocal8Bit() );
	ba.append( '\n' );
	proc_->write( ba );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool					HelpViewer::startAssistant		( void )
{
	if ( proc_ == false )
	{
		proc_ = new QProcess();

		connect( proc_, SIGNAL( error(QProcess::ProcessError) ),
				 this, SLOT( error(QProcess::ProcessError) )
				 );
		connect( proc_, SIGNAL( finished(int,QProcess::ExitStatus) ),
				 this, SLOT( finished(int,QProcess::ExitStatus) )
				 );
		connect( proc_, SIGNAL( readyReadStandardError() ),
				 this, SLOT( readyReadStandardError() )
				 );
		connect( proc_, SIGNAL( readyReadStandardOutput() ),
				 this, SLOT( readyReadStandardOutput() )
				 );
		connect( proc_, SIGNAL( started() ),
				 this, SLOT( started() )
				 );
		connect( proc_, SIGNAL( stateChanged(QProcess::ProcessState) ),
				 this, SLOT( stateChanged(QProcess::ProcessState) )
				 );

	}

	if (proc_->state() != QProcess::Running)
	{

		QString hlp_file = AppPaths::unique()->helpPath();
		hlp_file.append( "/cpg.qhc" );

		QString app = AppPaths::unique()->assistantPath();
		if ( app.isEmpty() == false )
			app.append( '/' );
#if !defined(Q_OS_MAC)
		app += QLatin1String( "assistant" );
#else
		app += QLatin1String( "Assistant.app/Contents/MacOS/Assistant" );
#endif

		QStringList args;
		args << QLatin1String("-collectionFile")
			 << hlp_file.toLatin1()
			 << QLatin1String("-enableRemoteControl");

		proc_->start( app, args );

		if ( proc_->waitForStarted() == false )
		{
			QMessageBox::critical(
						0, QObject::tr("CadPlayG error"),
						QObject::tr( "Unable to launch Qt Assistant (%1)").arg( app ) );
			return false;
		}

	}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::error ( QProcess::ProcessError error )
{
	Q_UNUSED( error );
	PGDBG( PGDBG_A_HELP, "HelpViewer::error" );
	PGDBG2( PGDBG_A_HELP, "  - code", error );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::finished (
		int exitCode, QProcess::ExitStatus exitStatus )
{
	Q_UNUSED( exitCode );
	Q_UNUSED( exitStatus );
	PGDBG( PGDBG_A_HELP, "HelpViewer::finished" );
	PGDBG2( PGDBG_A_HELP, "  - code", exitCode );
	PGDBG2( PGDBG_A_HELP, "  - status", exitStatus );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::readyReadStandardError ( void )
{
	PGDBG( PGDBG_A_HELP, "HelpViewer::readyReadStandardError" );
	PGDBG2( PGDBG_A_HELP, "  - error: ", proc_->readAllStandardError() );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::readyReadStandardOutput ( void )
{
	PGDBG( PGDBG_A_HELP, "HelpViewer::readyReadStandardOutput" );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::started		( void )
{
	PGDBG( PGDBG_A_HELP, "HelpViewer::started" );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					HelpViewer::stateChanged (
		QProcess::ProcessState newState )
{
	Q_UNUSED( newState );
	PGDBG( PGDBG_A_HELP, "HelpViewer::stateChanged" );
	PGDBG2( PGDBG_A_HELP, "  - new state", newState );

}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
